<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_minDistanceCalculation"></title>
    <style>
      .ol-popup {
          position: absolute;
          background-color: white;
          -webkit-filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
          filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
          padding: 15px;
          border-radius: 10px;
          border: 1px solid #cccccc;
          bottom: 12px;
          left: -50px;
          min-width: 260px;
      }

      .ol-popup:after, .ol-popup:before {
          top: 100%;
          border: solid transparent;
          content: " ";
          height: 0;
          width: 0;
          position: absolute;
          pointer-events: none;
      }

      .ol-popup:after {
          border-top-color: white;
          border-width: 10px;
          left: 48px;
          margin-left: -10px;
      }

      .ol-popup:before {
          border-top-color: #cccccc;
          border-width: 11px;
          left: 48px;
          margin-left: -11px;
      }
      #toolbar {
          position: absolute;
          top: 50px;
          right: 10px;
          width: 280px;
          text-align: center;
          z-index: 100;
          border-radius: 4px;
      }
  </style>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%; position: absolute;top: 0;">
<div id="toolbar" class="panel panel-primary">
  <div class='panel-heading'>
      <h5 class='panel-title text-center' data-i18n="resources.text_minDistanceCalculation"></h5></div>
  <div class='panel-body content'>
      <div class='panel'>
        <div class='input-group'>
            <span class='input-group-addon' data-i18n="resources.text_minDis"></span>
            <input type='number' class='form-control' id='minDistance' value='0'/>
        </div>
      </div>
      <div class='panel'>
      <div class='input-group'>
          <span class='input-group-addon' data-i18n="resources.text_maxDis"></span>
          <input type='number' class='form-control' id='maxDistance' value='-1'/>
      </div>
  </div>
      <input type="button" class="btn btn-default" data-i18n="[value]resources.btn_minDistanceCalculation"
              onclick="geometryMinDistanceAnalystProcess()"/>&nbsp;
      <input type="button" class="btn btn-default" data-i18n="[value]resources.btn_clearLayer"
              onclick="removeLayer()"/>
  </div>
</div>
<div id="map" style="width: 100%;height:100%"></div>
<div id="popup" class="ol-popup">
  <div id="popup-content"></div>
</div>
<script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" src="../../dist/ol/include-ol.js"></script>
<script type="text/javascript">
    var map, options, pointGeometryData, lineVectorSource,lineResultLayer,
    baseUrl = (window.isLocal ? window.server : "https://iserver.supermap.io")+"/iserver/services/map-changchun/rest/maps/长春市区图",
        serviceUrl = (window.isLocal ? window.server : "https://iserver.supermap.io")+"/iserver/services/spatialanalyst-changchun/restjsr/spatialanalyst";
    var container = document.getElementById('popup');
    var content = document.getElementById('popup-content');
    var overlay = new ol.Overlay(({
        element: container,
        autoPan: true,
        autoPanAnimation: {
            duration: 250
        }
    }));
    var extent = [48.4, -7668.25, 8958.85, -55.58];
    var projection = new ol.proj.Projection({
        code:'',
        extent: extent,
        units: 'm'
    });
    new ol.supermap.MapService(baseUrl).getMapInfo(function (serviceResult) {
        var mapJSONObj = serviceResult.result;
        map = new ol.Map({
            target: 'map',
            // ol v7版本用法为ol.control.defaults.defaults； v6版本以下用法为ol.control.defaults
            controls: ol.control.defaults.defaults({attributionOptions: {collapsed: false}})
                .extend([new ol.supermap.control.Logo()]),
            view: new ol.View({
                center: [5000, -3700],
                zoom: 2,
                projection: projection,
                multiWorld: true
            }),
            overlays: [overlay]
        });
        options = ol.source.TileSuperMapRest.optionsFromMapJSON(baseUrl, mapJSONObj);
        var layer = new ol.layer.Tile({
            source: new ol.source.TileSuperMapRest(options)
        });
        map.addLayer(layer);
        addReference();
        addCalcPoint();
    });

    function geometryMinDistanceAnalystProcess() {
      removeLayer();
      var minDistance = parseFloat(document.getElementById("minDistance").value);
      var maxDistance = parseFloat(document.getElementById("maxDistance").value);
      // 进行分析，查找距离参考数据集的最近距离
      var minDistanceAnalystParameters = new ol.supermap.GeometryMinDistanceAnalystParameters({
          inputGeometries: [pointGeometryData],
          referenceDatasetName: "Park@Changchun",
          createResultDataset: false,
          minDistance: minDistance,
          maxDistance: maxDistance,
      });
      new ol.supermap.SpatialAnalystService(serviceUrl).minDistanceAnalysis(minDistanceAnalystParameters).then(function (serviceResult) {
          // 检查参数，给予错误信息提示
          if(serviceResult.error){
            return widgets.alert.showAlert(serviceResult.error.errorMsg,false);
          }
          var minDistanceAnalystResult = serviceResult.result.distanceResults
          var resultMsg = serviceResult.result.message;
          if(!minDistanceAnalystResult) {
            return widgets.alert.showAlert(resultMsg || resources.msg_minDistance,false);
          }
          var innerHTML = resources.text_minDistanceIs + minDistanceAnalystResult[0].distance;
          content.innerHTML = innerHTML;
          overlay.setPosition(pointGeometryData.geometry.coordinates);
          var referenceIds = minDistanceAnalystResult[0].referenceGeometryIDs
          addPathLine(referenceIds)
      });
    }
    // 添加被计算的点
    function addCalcPoint() {
      pointGeometryData = {
        "type": "Feature",
        "geometry": {
          "type": "Point",
          "coordinates": [2000, -3500]
        }
      };
      var pointSource = new ol.source.Vector({
          features: (new ol.format.GeoJSON()).readFeatures(pointGeometryData)
      });
      var vectorLayer = new ol.layer.Vector({
          source: pointSource,
          style: new ol.style.Style({
            image: new ol.style.Icon(({
              src: '../img/marker.png'
            }))
          }),
          zIndex: 9
      });
      map.addLayer(vectorLayer);
    }
    // 添加参考数据集的要素
    function addReference() { 
      var sqlParam = new ol.supermap.QueryBySQLParameters({
          toIndex: -1,
          queryParams: {
              name: "Park@Changchun",
              attributeFilter: "SMID >= 0 "
          }
      });
      new ol.supermap.QueryService(baseUrl).queryBySQL(sqlParam).then(function (serviceResult) {
          var vectorSource = new ol.source.Vector({
              features: (new ol.format.GeoJSON()).readFeatures(serviceResult.result.recordsets[0].features),
              wrapX: false
          });
          resultLayer = new ol.layer.Vector({
              source: vectorSource,
              style: new ol.style.Style({
                image: new ol.style.Icon(({
                  src: '../img/marker-gold.png'
                }))
              }),
              zIndex: 9
          });
          map.addLayer(resultLayer);
      });
    }
    // 创建被计算对象与参考对象之间的连线
    function addPathLine(referenceIds) { 
      var referenceIdsStr = '(' + referenceIds.join(', ') + ')';
      var sqlParam = new ol.supermap.QueryBySQLParameters({
          toIndex: -1,
          queryParams: {
              name: "Park@Changchun",
              attributeFilter: "SMID IN " + referenceIdsStr
          }
      });
      new ol.supermap.QueryService(baseUrl).queryBySQL(sqlParam).then(function (serviceResult) {
        var resFrature = serviceResult.result.recordsets[0].features;
        var lineFeature = new ol.Feature({
          geometry: new ol.geom.LineString([
            resFrature.features[0].geometry.coordinates,
            pointGeometryData.geometry.coordinates,
          ])
        });
        lineVectorSource = new ol.source.Vector()
        lineVectorSource.addFeature(lineFeature);
        lineResultLayer = new ol.layer.Vector({
            source: lineVectorSource,
            style: new ol.style.Style({
              stroke: new ol.style.Stroke({
                color: '#304DBE',
                width: 2
              })
            })
        });
        map.addLayer(lineResultLayer);
      });
    }
    //移除结果图层
    function removeLayer() {
      widgets.alert.clearAlert();
      if (map.getLayers().getLength() > 3) {
          map.removeLayer(lineResultLayer);
          closeOverlay()
      }
    }
    //关闭弹窗
    function closeOverlay() {
      overlay.setPosition(undefined);
      return false
    };
</script>
</body>
</html>